import lyRequest from '@/service'

/***
 * 请求页面数据
 * @param pageName 页面名字
 * @param info 查询参数
 * 用户页、角色页、菜单页都是/${pageName}/list即可x
 */
export function fetchPageData(pageName: string, info: any) {
  let url = `/${pageName}/list`
  if (pageName === 'merchantApply') url = '/merchant/applyList'
  if (pageName === 'merchantUpgrade') url = '/merchant/upgrade/list'
  if (pageName === 'loginLog') url = '/system/log/loginList'
  if (pageName === 'operationLog') url = '/system/log/list'
  if (pageName === 'banned') url = '/user/banned/list'
  if (pageName === 'admin') url = '/user/admin/list'
  if (pageName === 'goodBrand') url = '/brand/getBrandList'
  if (pageName === 'goodSku') {
    url = '/sku/getGoodSkuList'
    info.productId = info.productId ? info.productId : -1
    Reflect.deleteProperty(info, 'merchantId')
  }
  if (pageName === 'goodClassify') {
    url = '/classify/getClassifyList'
    const merchantId = info.merchantId ? info.merchantId : -1
    info = { name: info.name, merchantId }
  }
  return lyRequest.get({
    url,
    params: info,
    isShowLoading: true
  })
}

/***
 * 不同页面获取500条数据的请求
 * @param pageName
 * @param status 2为批量删除的id 1为获取正常账号的id 0获取封禁id
 * @param isMerchant 批量驳回用到
 */
export function getBatchIds(
  pageName: string,
  status?: number,
  isMerchant?: boolean
) {
  let url
  switch (pageName) {
    case 'user':
      url = `/user/getBatchIds/0/${status}`
      break
    case 'admin':
      url = `/user/getBatchIds/2/${status}`
      break
    case 'merchantType':
      url = `/merchantType/getIdList`
      break
    case 'merchant':
      url = `/merchant/getIdList/${status}`
      break
    case 'merchantUpgrade':
      if (isMerchant) {
        url = `/merchant/upgrade/getBatchAcceptData`
      } else url = `/merchant/upgrade/getUpgradeIds`
      break
    case 'merchantApply':
      if (isMerchant) {
        url = `/merchant/getMerchantIdApplyList`
      } else url = `/merchant/getUserIdApplyList`
      break
  }
  return lyRequest.get({ url })
}

/***
 * 批量删除数据
 * 注意：经营页面和用户页比较特殊，前者路径不一样，后者需要多带safeType参数
 */
export function reqBatchDeleteData(ids: number[], pageName: string) {
  let url
  let safeType

  pageName === 'user' && (safeType = 'batch-user-delete')
  pageName === 'merchant' && (safeType = 'batch-merchant-delete')
  pageName === 'merchantApply' && (safeType = 'batch-merchant-apply-delete')
  pageName === 'admin' && (safeType = 'batch-admin-delete')
  // pageName === 'role' && (safeType = 'batch-role-delete') //角色管理页

  if (
    pageName === 'user' ||
    pageName === 'merchant' ||
    pageName === 'merchantApply' ||
    pageName === 'admin'
  ) {
    url = `/user/deleteBatch`
  }
  if (pageName === 'merchantType') {
    url = `/${pageName}/batchDelete`
  }
  if (pageName === 'merchantUpgrade') {
    url = `/merchant/upgrade/batch/delete`
  }

  return lyRequest.post({
    url,
    data:
      pageName === 'user' ||
      pageName === 'merchant' ||
      pageName === 'merchantApply' ||
      pageName === 'admin'
        ? { ids, safeType }
        : ids,
    isShowMsg: true
  })
}

/***
 * 删除数据
 */
export function reqDeleteData(id: number, pageName: string, skuType?: number) {
  let url
  let safeType
  let method = 'delete'
  let data
  if (
    pageName === 'user' ||
    pageName === 'merchant' ||
    pageName === 'merchantApply' ||
    pageName === 'admin'
  ) {
    // 有几个页面的账号有关请求都一样，只是safeType不一样
    if (pageName === 'user') safeType = 'user-delete'
    if (pageName === 'merchant') safeType = 'merchant-delete'
    if (pageName === 'merchantApply') safeType = 'merchant-apply-delete'
    if (pageName === 'admin') safeType = 'admin-delete'
    url = '/user/delete'
    method = 'post'
    data = { userId: id, safeType }
  } else if (pageName === 'merchantUpgrade') {
    url = `/merchant/upgrade/${id}`
    data = {}
  } else if (pageName === 'goodSku') {
    url = `/sku/sku/${skuType}/${id}`
    data = {}
  } else {
    pageName === 'goodBrand' && (pageName = 'brand')
    pageName === 'goodClassify' && (pageName = 'classify')
    url = `/${pageName}/${id}`
    data = {}
  }
  return lyRequest[method]({
    url,
    data,
    isShowMsg: true
  })
}

/***
 * 批量封禁账号 该接口适用于 用户管理封禁 商户管理封禁 管理员管理封禁
 * @param ids 封禁的id集合
 * @param bannedTime 封禁时间
 */
export function reqBatchBannedByIds(ids: number[], bannedTime: number) {
  return lyRequest.post({
    url: '/user/batch/banned',
    data: {
      userIdList: ids,
      bannedTime
    },
    isShowMsg: true
  })
}

/***
 * 批量解封 该接口适用于 用户管理封禁 商户管理封禁 管理员管理封禁
 * @param ids 解封的id集合
 */
export function reqBatchUntieDisable(ids: number[]) {
  return lyRequest.post({
    url: '/user/batchUntieDisable',
    data: ids,
    isShowMsg: true
  })
}

// 更新信息
export function updatePageInfo(pageName: string, info: any) {
  pageName === 'admin' && (pageName = 'user')
  pageName === 'goodBrand' && (pageName = 'brand')
  pageName === 'goodSku' && (pageName = 'sku')
  pageName === 'goodClassify' && (pageName = 'classify')
  return lyRequest.put({
    url: `/${pageName}`,
    data: info,
    isShowMsg: true
  })
}

// 新增数据
export function reqAddPageData(pageName: string, info: any) {
  let url = `/${pageName}`
  pageName === 'role' && (url = '/role')
  pageName === 'admin' && (url = '/user/admin')
  pageName === 'goodBrand' && (url = '/brand')
  pageName === 'goodClassify' && (url = '/classify')
  if (pageName === 'goodSku') {
    if (info.productId) {
      url = '/sku'
    } else {
      url = '/sku/addValue'
    }
  }
  return lyRequest.post({
    url,
    data: info,
    isShowMsg: true
  })
}

/***
 * 单个通过申请
 */
export function reqPassMerchant(
  pageName: string,
  id: number,
  applyStoreCompanyType?: number
) {
  let url
  if (pageName === 'merchantApply') {
    url = `/merchant/accept/${id}`
  }
  if (pageName === 'merchantUpgrade' && applyStoreCompanyType) {
    url = `/merchant/upgrade/accept/${id}/${applyStoreCompanyType}`
  }
  return lyRequest.post({
    url,
    isShowMsg: true
  })
}

/***
 * 驳回申请
 */
export function reqRejectMerchant(pageName: string, info: any) {
  let url
  if (pageName === 'merchantApply') url = `/merchant/reject`
  if (pageName === 'merchantUpgrade') url = `/merchant/upgrade/reject`
  // if(pageName === 'merchantApply') url = `/merchant/upgrade/reject`
  return lyRequest.post({
    url,
    data: info,
    isShowMsg: true
  })
}

/***
 * 批量通过
 */
export function reqBatchPassMerchant(pageName: string, ids: number[]) {
  let url
  const info = ids
  pageName === 'merchantApply' && (url = `/merchant/batchAccept`)
  pageName === 'merchantUpgrade' && (url = `/merchant/upgrade/batchAccept`)
  return lyRequest.post({
    url,
    data: info,
    isShowMsg: true
  })
}

/**
 * 图片上传
 */

export function uploadImage(pageName: string, file: any) {
  if (pageName === 'goodSku' || pageName === 'goodClassify') {
    pageName = 'sku'
  }
  return lyRequest.post({
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    url: `/${pageName}/uploadImage`,
    data: file
  })
}
